/*********************************************************************
	Rhapsody	: 7.5.2
	Login		: michalwski
	Component	: Lib
	Configuration 	: DefaultConfig
	Model Element	: RobotController
//!	Generated Date	: Sat, 8, Jan 2011 
	File Path	: Lib/DefaultConfig/RobotPkg/RobotController.java
*********************************************************************/

package RobotPkg;

//## auto_generated
import java.util.*;
//## auto_generated
import com.ibm.rational.rhapsody.oxf.*;
//## auto_generated
import com.ibm.rational.rhapsody.oxf.states.*;

//----------------------------------------------------------------------------
// RobotPkg/RobotController.java                                                                  
//----------------------------------------------------------------------------

//## package RobotPkg 


//## class RobotController 
public class RobotController implements RiJStateConcept {
    
    public Reactive reactive;		//## ignore 
    
    protected LinkedList<IRobotListener> listeners;		//## attribute listeners 
    
    protected ArrayList<Engine> itsEngine;		//## classInstance itsEngine 
    
    protected ArrayList<Sensor> itsSensor;		//## classInstance itsSensor 
    
    //#[ ignore 
    public static final int RiJNonState=0;
    public static final int on=1;
    public static final int stable=2;
    public static final int processing=3;
    public static final int off=4;
    //#]
    protected int rootState_subState;		//## ignore 
    
    protected int rootState_active;		//## ignore 
    
    protected int on_subState;		//## ignore 
    
    
    //## statechart_method 
    public RiJThread getThread() {
        return reactive.getThread();
    }
    
    //## statechart_method 
    public void schedTimeout(long delay, long tmID, RiJStateReactive reactive) {
        getThread().schedTimeout(delay, tmID, reactive);
    }
    
    //## statechart_method 
    public void unschedTimeout(long tmID, RiJStateReactive reactive) {
        getThread().unschedTimeout(tmID, reactive);
    }
    
    //## statechart_method 
    public boolean isIn(int state) {
        return reactive.isIn(state);
    }
    
    //## statechart_method 
    public boolean isCompleted(int state) {
        return reactive.isCompleted(state);
    }
    
    //## statechart_method 
    public RiJEventConsumer getEventConsumer() {
        return (RiJEventConsumer)reactive;
    }
    
    //## statechart_method 
    public void gen(RiJEvent event) {
        reactive._gen(event);
    }
    
    //## statechart_method 
    public void queueEvent(RiJEvent event) {
        reactive.queueEvent(event);
    }
    
    //## statechart_method 
    public int takeEvent(RiJEvent event) {
        return reactive.takeEvent(event);
    }
    
    // Constructors
    
    //## auto_generated 
    public  RobotController(RiJThread p_thread) {
        reactive = new Reactive(p_thread);
        {
            listeners = new LinkedList<IRobotListener>();
        }
        {
            itsEngine = new ArrayList<Engine>();
        }
        {
            itsSensor = new ArrayList<Sensor>();
        }
        initRelations(p_thread);
    }
    
    //## operation addListener(IRobotListener) 
    public void addListener(final IRobotListener listener) {
        //#[ operation addListener(IRobotListener) 
        listeners.add(listener);
        //#]
    }
    
    //## operation getSensor(int) 
    public Sensor getSensor(int sensorId) {
        //#[ operation getSensor(int) 
                     return this.itsSensor.get(sensorId);
        //#]
    }
    
    //## operation getSensors() 
    public ArrayList<Sensor> getSensors() {
        //#[ operation getSensors() 
        return itsSensor;
        //#]
    }
    
    //## operation left() 
    public void left() {
        //#[ operation left() 
        itsEngine.get(0).gen(new evChangeDirection());   
        itsEngine.get(1).gen(new evChangeDirection());
        itsEngine.get(0).setSpeed(0);
        itsEngine.get(1).setSpeed(1);
        itsEngine.get(1).setDirection(1); //1 do przodu
        itsEngine.get(1).gen(new evMove());
        itsEngine.get(0).gen(new evMove());
        
        //#]
    }
    
    //## operation process(int,boolean) 
    public void process(int sensorId, boolean line) {
        //#[ operation process(int,boolean) 
        System.out.println(itsSensor.get(0).getLine()+","+
        		itsSensor.get(1).getLine()+","+
        		itsSensor.get(2).getLine()+","+
        		itsSensor.get(3).getLine()+","+
        		itsSensor.get(4).getLine()+","+
        		itsSensor.get(5).getLine());
        if(itsSensor.get(2).getLine() == true || itsSensor.get(3).getLine() == true){
        	//itsEngine.get(0).setDirection(false);
        	//itsEngine.get(1).setDirection(false);
        	System.out.println("Wszystko ok obydwa silniki do przodu");
        	straight();
        }                                                           
        else if(itsSensor.get(0).getLine() == true || itsSensor.get(1).getLine() == true){
        	//itsEngine.get(0).setDirection(true);
        	//itsEngine.get(1).setDirection(false);
        	System.out.println("Skret w lewo");
        	left();
        }                                  
        else if (itsSensor.get(4).getLine() == true || itsSensor.get(5).getLine() == true){
        	//itsEngine.get(0).setDirection(false);
        	//itsEngine.get(1).setDirection(true);
        	System.out.println("Skret w prawo");
        	right();
        }                     
        else {
        	System.out.println("Tu cos tylko co???");
        }              
        //#]
    }
    
    //## operation right() 
    public void right() {
        //#[ operation right() 
        itsEngine.get(0).gen(new evChangeDirection());
        itsEngine.get(1).gen(new evChangeDirection());
        itsEngine.get(1).setSpeed(0);
        itsEngine.get(0).setSpeed(1);
        itsEngine.get(0).setDirection(1); //1 do przodu
        itsEngine.get(1).gen(new evMove()); 
        itsEngine.get(0).gen(new evMove());
        
        //#]
    }
    
    //## operation setup() 
    public void setup() {
        //#[ operation setup() 
        for(int i=0; i<itsSensor.size(); i++){
         itsSensor.get(i).id = i; 
         itsSensor.get(i).gen(new evOnOff());
        }  
        
        for(int i=0; i<itsEngine.size(); i++) { 
         itsEngine.get(i).id = i; 
         itsEngine.get(i).gen(new evOnOff());
        }
        
        
        
        //#]
    }
    
    //## operation shutdown() 
    public void shutdown() {
        //#[ operation shutdown() 
        for(int i=0; i<itsSensor.size(); i++){
         itsSensor.get(i).gen(new evOnOff());
        }  
        
        for(int i=0; i<itsEngine.size(); i++) {
         itsEngine.get(i).gen(new evOnOff());
        }
        //#]
    }
    
    //## operation straight() 
    public void straight() {
        //#[ operation straight() 
        itsEngine.get(0).gen(new evChangeDirection());
        itsEngine.get(1).gen(new evChangeDirection());
        itsEngine.get(1).setSpeed(1);
        itsEngine.get(0).setSpeed(1);
        itsEngine.get(0).setDirection(1); //1 do przodu  
        itsEngine.get(1).setDirection(1); //1 do przodu
        itsEngine.get(1).gen(new evMove()); 
        itsEngine.get(0).gen(new evMove());
        
        //#]
    }
    
    //## auto_generated 
    public ListIterator<IRobotListener> getListeners() {
        ListIterator<IRobotListener> iter = listeners.listIterator();
        return iter;
    }
    
    //## auto_generated 
    public void setListeners(IRobotListener p_listeners) {
        listeners.add(p_listeners);
    }
    
    //## auto_generated 
    public ListIterator<Engine> getItsEngine() {
        ListIterator<Engine> iter = itsEngine.listIterator();
        return iter;
    }
    
    //## auto_generated 
    public void _addItsEngine(Engine p_Engine) {
        itsEngine.add(p_Engine);
    }
    
    //## auto_generated 
    public Engine newItsEngine() {
        Engine newEngine = new Engine(getThread());
        newEngine._setItsRobotController(this);
        itsEngine.add(newEngine);
        return newEngine;
    }
    
    //## auto_generated 
    public void _removeItsEngine(Engine p_Engine) {
        itsEngine.remove(p_Engine);
    }
    
    //## auto_generated 
    public void deleteItsEngine(Engine p_Engine) {
        p_Engine._setItsRobotController(null);
        itsEngine.remove(p_Engine);
        p_Engine=null;
    }
    
    //## auto_generated 
    public ListIterator<Sensor> getItsSensor() {
        ListIterator<Sensor> iter = itsSensor.listIterator();
        return iter;
    }
    
    //## auto_generated 
    public void _addItsSensor(Sensor p_Sensor) {
        itsSensor.add(p_Sensor);
    }
    
    //## auto_generated 
    public Sensor newItsSensor() {
        Sensor newSensor = new Sensor(getThread());
        newSensor._setItsRobotController(this);
        itsSensor.add(newSensor);
        return newSensor;
    }
    
    //## auto_generated 
    public void _removeItsSensor(Sensor p_Sensor) {
        itsSensor.remove(p_Sensor);
    }
    
    //## auto_generated 
    public void deleteItsSensor(Sensor p_Sensor) {
        p_Sensor._setItsRobotController(null);
        itsSensor.remove(p_Sensor);
        p_Sensor=null;
    }
    
    //## auto_generated 
    protected void initRelations(RiJThread p_thread) {
        {
            for (int i = 0; i < 2; i++) 
                newItsEngine();
            
        }
        {
            for (int i = 0; i < 6; i++) 
                newItsSensor();
            
        }
    }
    
    //## auto_generated 
    public boolean startBehavior() {
        boolean done = true;
        {
            ListIterator<Engine> iter = itsEngine.listIterator();
            while (iter.hasNext()){
                done &= (itsEngine.get(iter.nextIndex())).startBehavior();
                iter.next();
            }
        }
        {
            ListIterator<Sensor> iter = itsSensor.listIterator();
            while (iter.hasNext()){
                done &= (itsSensor.get(iter.nextIndex())).startBehavior();
                iter.next();
            }
        }
        done &= reactive.startBehavior();
        return done;
    }
    
    //## ignore 
    public class Reactive extends RiJStateReactive {
        
        // Default constructor 
        public Reactive() {
            this(RiJMainThread.instance());
        }
        
        
        // Constructors
        
        public  Reactive(RiJThread p_thread) {
            super(p_thread);
            initStatechart();
        }
        
        //## statechart_method 
        public boolean isIn(int state) {
            if(on_subState == state)
                {
                    return true;
                }
            if(rootState_subState == state)
                {
                    return true;
                }
            return false;
        }
        
        //## statechart_method 
        public boolean isCompleted(int state) {
            return true;
        }
        
        //## statechart_method 
        public void rootState_entDef() {
            {
                rootState_enter();
                rootStateEntDef();
            }
        }
        
        //## statechart_method 
        public int rootState_dispatchEvent(short id) {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            switch (rootState_active) {
                case off:
                {
                    res = off_takeEvent(id);
                }
                break;
                case stable:
                {
                    res = stable_takeEvent(id);
                }
                break;
                case processing:
                {
                    res = processing_takeEvent(id);
                }
                break;
                default:
                    break;
            }
            return res;
        }
        
        //## auto_generated 
        protected void initStatechart() {
            rootState_subState = RiJNonState;
            rootState_active = RiJNonState;
            on_subState = RiJNonState;
        }
        
        //## statechart_method 
        public void offEnter() {
            //#[ state ROOT.off.(Entry) 
            for(IRobotListener l : listeners){
              l.robotOff();
            }
            //#]
        }
        
        //## statechart_method 
        public int processingTakeNull() {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            processing_exit();
            stable_entDef();
            res = RiJStateReactive.TAKE_EVENT_COMPLETE;
            return res;
        }
        
        //## statechart_method 
        public void stableExit() {
        }
        
        //## statechart_method 
        public void processingEnter() {
        }
        
        //## statechart_method 
        public void off_exit() {
            offExit();
        }
        
        //## statechart_method 
        public void offExit() {
        }
        
        //## statechart_method 
        public int stableTakeevLine() {
            evLine params = (evLine) event;
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            stable_exit();
            //#[ transition 3 
            process(params.id, params.line);
            //#]
            processing_entDef();
            res = RiJStateReactive.TAKE_EVENT_COMPLETE;
            return res;
        }
        
        //## statechart_method 
        public void onEnter() {
            //#[ state ROOT.on.(Entry) 
            for(IRobotListener l : listeners){
              l.robotOn();
            }
            //#]
        }
        
        //## statechart_method 
        public void processing_exit() {
            popNullConfig();
            processingExit();
        }
        
        //## statechart_method 
        public void processingExit() {
        }
        
        //## statechart_method 
        public void stable_entDef() {
            stable_enter();
        }
        
        //## statechart_method 
        public int rootState_takeEvent(short id) {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            return res;
        }
        
        //## statechart_method 
        public void on_exit() {
            switch (on_subState) {
                case stable:
                {
                    stable_exit();
                }
                break;
                case processing:
                {
                    processing_exit();
                }
                break;
                default:
                    break;
            }
            on_subState = RiJNonState;
            onExit();
        }
        
        //## statechart_method 
        public void on_entDef() {
            on_enter();
            
            stable_entDef();
        }
        
        //## statechart_method 
        public int on_takeEvent(short id) {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            if(event.isTypeOf(evOnOff.evOnOff_RobotPkg_id))
                {
                    res = onTakeevOnOff();
                }
            
            return res;
        }
        
        //## statechart_method 
        public void rootState_enter() {
            rootStateEnter();
        }
        
        //## statechart_method 
        public void rootStateEnter() {
        }
        
        //## statechart_method 
        public int offTakeevOnOff() {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            off_exit();
            //#[ transition 1 
            setup();
            System.out.println("Robot On");
            //#]
            on_entDef();
            res = RiJStateReactive.TAKE_EVENT_COMPLETE;
            return res;
        }
        
        //## statechart_method 
        public void off_enter() {
            rootState_subState = off;
            rootState_active = off;
            offEnter();
        }
        
        //## statechart_method 
        public int stable_takeEvent(short id) {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            if(event.isTypeOf(evLine.evLine_RobotPkg_id))
                {
                    res = stableTakeevLine();
                }
            
            if(res == RiJStateReactive.TAKE_EVENT_NOT_CONSUMED)
                {
                    res = on_takeEvent(id);
                }
            return res;
        }
        
        //## statechart_method 
        public void onExit() {
        }
        
        //## statechart_method 
        public int processing_takeEvent(short id) {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            if(event.isTypeOf(RiJEvent.NULL_EVENT_ID))
                {
                    res = processingTakeNull();
                }
            
            if(res == RiJStateReactive.TAKE_EVENT_NOT_CONSUMED)
                {
                    res = on_takeEvent(id);
                }
            return res;
        }
        
        //## statechart_method 
        public void stable_enter() {
            on_subState = stable;
            rootState_active = stable;
            stableEnter();
        }
        
        //## statechart_method 
        public void stableEnter() {
        }
        
        //## statechart_method 
        public void on_enter() {
            rootState_subState = on;
            onEnter();
        }
        
        //## statechart_method 
        public void rootState_exit() {
            switch (rootState_subState) {
                case off:
                {
                    off_exit();
                }
                break;
                case on:
                {
                    on_exit();
                }
                break;
                default:
                    break;
            }
            rootState_subState = RiJNonState;
            rootStateExit();
        }
        
        //## statechart_method 
        public void rootStateEntDef() {
            off_entDef();
        }
        
        //## statechart_method 
        public int off_takeEvent(short id) {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            if(event.isTypeOf(evOnOff.evOnOff_RobotPkg_id))
                {
                    res = offTakeevOnOff();
                }
            
            return res;
        }
        
        //## statechart_method 
        public void processing_entDef() {
            processing_enter();
        }
        
        //## statechart_method 
        public int onTakeevOnOff() {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            on_exit();
            //#[ transition 5 
            shutdown();
            System.out.println("Robot Off");
            //#]
            off_entDef();
            res = RiJStateReactive.TAKE_EVENT_COMPLETE;
            return res;
        }
        
        //## statechart_method 
        public void stable_exit() {
            stableExit();
        }
        
        //## statechart_method 
        public void rootStateExit() {
        }
        
        //## statechart_method 
        public void processing_enter() {
            pushNullConfig();
            on_subState = processing;
            rootState_active = processing;
            processingEnter();
        }
        
        //## statechart_method 
        public void off_entDef() {
            off_enter();
        }
        
    }
}
/*********************************************************************
	File Path	: Lib/DefaultConfig/RobotPkg/RobotController.java
*********************************************************************/

